<?php

namespace App\Admin\Controllers;

use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;

class UserTreeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'User';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'));
        $grid->column('email', __('Email'));
        $grid->column('email_verified_at', __('Email verified at'));
        $grid->column('password', __('Password'));
        $grid->column('remember_token', __('Remember token'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        $grid->column('role_id', __('Role id'));
        $grid->column('userid', __('Userid'));
        $grid->column('openid', __('Openid'));
        $grid->column('status', __('Status'));
        $grid->column('qr_code', __('Qr code'));
        $grid->column('type', __('Type'));
        $grid->column('gender', __('Gender'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('avatar', __('Avatar'));
        $grid->column('position', __('Position'));
        $grid->column('is_leader_in_dept', __('Is leader in dept'));
        $grid->column('session_key', __('Session key'));
        $grid->column('workshop_num', __('Workshop num'));
        $grid->column('tiyan', __('Tiyan'));
        $grid->column('parent_id', __('Parent id'));
        $grid->column('sort', __('Sort'));

        return $grid;
    }
    public function index(Content $content)
    {
        $tree = new Tree(new User());
        $tree->disableCreate();

        $tree->branch(function ($data) {
            $src = $data['avatar'];
            $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

            return "{$data['name']}-{$data['mobile']}-{$logo}";
        });
        return $content
            ->header('用户上下级')
            ->body($tree);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('email_verified_at', __('Email verified at'));
        $show->field('password', __('Password'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('role_id', __('Role id'));
        $show->field('userid', __('Userid'));
        $show->field('openid', __('Openid'));
        $show->field('status', __('Status'));
        $show->field('qr_code', __('Qr code'));
        $show->field('type', __('Type'));
        $show->field('gender', __('Gender'));
        $show->field('mobile', __('Mobile'));
        $show->field('avatar', __('Avatar'));
        $show->field('position', __('Position'));
        $show->field('is_leader_in_dept', __('Is leader in dept'));
        $show->field('session_key', __('Session key'));
        $show->field('workshop_num', __('Workshop num'));
        $show->field('tiyan', __('Tiyan'));
        $show->field('parent_id', __('Parent id'));
        $show->field('sort', __('Sort'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->email('email', __('Email'));
        $form->datetime('email_verified_at', __('Email verified at'))->default(date('Y-m-d H:i:s'));
        $form->password('password', __('Password'));
        $form->text('remember_token', __('Remember token'));
        $form->number('role_id', __('Role id'))->default(3);
        $form->text('userid', __('Userid'));
        $form->text('openid', __('Openid'));
        $form->number('status', __('Status'));
        $form->text('qr_code', __('Qr code'));
        $form->number('type', __('Type'));
        $form->text('gender', __('Gender'));
        $form->mobile('mobile', __('Mobile'));
        $form->image('avatar', __('Avatar'));
        $form->text('position', __('Position'));
        $form->number('is_leader_in_dept', __('Is leader in dept'));
        $form->text('session_key', __('Session key'));
        $form->number('workshop_num', __('Workshop num'));
        $form->number('tiyan', __('Tiyan'));
        $form->number('parent_id', __('Parent id'));
        $form->text('sort', __('Sort'));

        return $form;
    }
}
